//
//  CampusPreviewVideoListViewController.m
//  ShowYork
//
//  Created by hxh on 2021/3/26.
//  Copyright © 2021 hxh. All rights reserved.
//

#import "CampusPreviewPicListViewController.h"
#import "CampusPreviewPicItemCell.h"
#import "CampusSchoolPictureItemModel.h"
#import "CampusPicBroswerViewController.h"
@interface CampusPreviewPicListViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>
@property (nonatomic,strong)UICollectionView *collectionView;
@property (nonatomic,assign)NSInteger pageIndex;
@property (nonatomic,strong)NSMutableArray *dataArray;

@property (nonatomic,assign)BOOL lastPage;
@property (nonatomic,assign)NSInteger total;
@end

@implementation CampusPreviewPicListViewController

- (NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];

    UIImageView* blackLineImageView = [self findHairlineImageViewUnder:self.navigationController.navigationBar];
    blackLineImageView.hidden = YES;
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    UIImageView* blackLineImageView = [self findHairlineImageViewUnder:self.navigationController.navigationBar];
    blackLineImageView.hidden = NO;

}


- (void)viewDidLoad {
    [super viewDidLoad];
  
    [self setUI];
}
- (void)setUI{
    
    self.navigationItem.title = @"校园图集";
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];

    layout.sectionInset = UIEdgeInsetsMake(16,16, 0,16);
    layout.itemSize = CGSizeMake((kScreenW - 32 - 8)/2,(kScreenW - 32 - 8)/2);
    layout.minimumLineSpacing = 8.0;
    layout.minimumInteritemSpacing = 4.0f;
    
    self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:layout];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.showsVerticalScrollIndicator = NO;
    self.collectionView.showsHorizontalScrollIndicator = NO;
    self.collectionView.backgroundColor = [UIColor whiteColor];
    
    [self.collectionView registerClass:[CampusPreviewPicItemCell class] forCellWithReuseIdentifier:@"CampusPreviewPicItemCell"];
    
    
    [self.view addSubview:self.collectionView];
    
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.bottom.equalTo(self.view);
    }];
    
    self.pageIndex = 1;
    
    __weak typeof(self)weakSelf = self;
    MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        weakSelf.pageIndex = 1;
        [weakSelf loadDataSource];
    }];
    
    MJRefreshAutoNormalFooter *footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        weakSelf.pageIndex++;
        [weakSelf loadDataSource];
    }];

    [footer setTitle:@"—— 没有更多了 ——" forState:MJRefreshStateNoMoreData];
    footer.stateLabel.font = font(10);
    footer.stateLabel.textColor = RGBA(153, 153, 153, 1);
    footer.hidden = YES;
    self.collectionView.mj_header = header;
    self.collectionView.mj_footer = footer;
    
    [self showDataLoadState];
    [self loadDataSource];
    
}

- (void)loadDataSource{
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    param[@"pageIndex"] = @(self.pageIndex);
    param[@"pageSize"] = @(10);
    param[@"id"] = self.schId;
    param[@"md5"] = [RPAppManager md5StrWithApi:SchoolPictureList];
    [[RPNetworkExtensionTool sharedTool] requestPOST:kGET_REQUEST_URL(SchoolPictureList) parames:param success:^(id responseObj) {
        [self hideDataLoadLoadState];
        [self stopRefresh];
        if (self.pageIndex == 1) {
            self.dataArray = [CampusSchoolPictureItemModel mj_objectArrayWithKeyValuesArray:responseObj[RESPONSE][LIST]];
        }else{
            [self.dataArray addObjectsFromArray:[CampusSchoolPictureItemModel mj_objectArrayWithKeyValuesArray:responseObj[RESPONSE][LIST]]];
        }
        
        self.collectionView.mj_footer.hidden = self.dataArray.count == 0;
        
        CGFloat bottom = self.collectionView.mj_insetB;
        if ([responseObj[RESPONSE][LASTPAGE]boolValue]) {
            [self.collectionView.mj_footer endRefreshingWithNoMoreData];
            self.lastPage = YES;
            if (self.collectionView.mj_footer.cl_height == 44.0f) {
                self.collectionView.mj_footer.cl_height = 138.0f;
                if (bottom == 44.0f || bottom == GK_SAVEAREA_BTM + 44.0f) {
                    self.collectionView.mj_insetB = 138.0f;
                }
            }
        }else{
            [self.collectionView.mj_footer resetNoMoreData];
            self.lastPage = NO;
            if (self.collectionView.mj_footer.cl_height == 138.0f) {
                self.collectionView.mj_footer.cl_height = 44.0f;
                if (bottom == 138.0f) {
                    self.collectionView.mj_insetB = 44.0f;
                }
            }
        }
        self.total = [responseObj[RESPONSE][TOTAL]intValue];
        
        [self.collectionView reloadData];
        
    } failure:^(id error) {
        [self hideDataLoadLoadState];
        [self stopRefresh];
    }];
}

- (void)stopRefresh{
    if ([self.collectionView.mj_header isRefreshing]) {
        [self.collectionView.mj_header endRefreshing];
    }
    if ([self.collectionView.mj_footer isRefreshing]) {
        [self.collectionView.mj_footer endRefreshing];
    }
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.dataArray.count;
}
-(__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    CampusPreviewPicItemCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CampusPreviewPicItemCell" forIndexPath:indexPath];
    [cell setModel:self.dataArray[indexPath.item]];
    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    [MobClick event:@"show_pic_detail"];
    
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    param[@"currentIndex"] = @(indexPath.item);
    param[@"currentPage"] = @(self.pageIndex);
    param[@"listSource"] = self.dataArray;
    param[@"total"] = @(self.total);
    param[@"schId"] = self.schId;
    param[@"lastPage"] = @(self.lastPage);

    CampusPicBroswerViewController *vc = [[CampusPicBroswerViewController alloc]initWithConfigInfo:param];
    [self.navigationController pushViewController:vc animated:YES];
}


- (UIImageView *)findHairlineImageViewUnder:(UIView *)view
{
    if ([view isKindOfClass:UIImageView.class] && view.bounds.size.height <= 1.0)
    {
        return (UIImageView *)view;
    }
    for (UIView *subview in view.subviews) {
        UIImageView *imageView = [self findHairlineImageViewUnder:subview];
        if (imageView) {
            return imageView;
        }
    }
    return nil;
}


@end
